/*******************************************************************************************
Copyright (C) 2010 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*******************************************************************************************/

/*jslint plusplus: true, indent: 4*/

function agilentCS_L_Date(aStr) {
	"use strict";

	if (aStr && typeof(aStr) === "string") {
		return new Date(aStr);
	}
	return undefined;
}

function agilentCS_G_Path(aPath, aFile) {
	"use strict";
	if (aPath && aFile) {
		return aPath + aFile;
	}
	if (aPath) {
		return aPath;
	}
	if (aFile) {
		return aFile;
	}
	return undefined;
}

function agilentCS_G_Date(aDate) {
	"use strict";
	var Months, caps, year, month, day, hour, min;
	Months = { "Jan": 0, "Feb": 1, "Mar": 2, "Apr": 3, "May": 4, "Jun": 5, "Jul": 6, "Aug": 7, "Sep": 8, "Oct": 9, "Nov": 10, "Dec": 11 };
	if (!aDate) {
		return undefined;
	}

	caps = aDate.match(/(\d{1,2})\s([A-Z][a-z]{2})\s(\d{4})\s{2}(\d{2}):(\d{2})/);
	if (!caps || caps.length !== 6) {
		return undefined;
	}
	year = parseInt(caps[3], 10);
	month = Months[caps[2]];
	day = parseInt(caps[1], 10);
	hour = parseInt(caps[4], 10);
	min = parseInt(caps[5], 10);
	return new Date(year, month, day, hour, min);
}

function agilentCS_Date() {
	"use strict";
	var i, curDate;
	for (i = 0; i < arguments.length; i++) {
		if (arguments[i] && typeof(arguments[i]) === "string") {
			curDate = agilentCS_L_Date(arguments[i].trim());
			if (curDate instanceof Date && !isNaN(curDate)) {
				return curDate;
			} else {
				curDate = agilentCS_G_Date(arguments[i].trim());
				if (curDate instanceof Date && !isNaN(curDate)) {
					return curDate;
				}
			}
		}
	}

	return undefined;
}

function agilentCS_G_Vial(aVial1, aVial2) {
	"use strict";
	if (!aVial1) {
		return aVial2;
	}
	if (!aVial2) {
		return aVial1;
	}
	return aVial1;
}

function massLynxDate(aDate, aTime) {
	"use strict";
	var Months, dCaps, tCaps, year, month, day, hour, min, sec;
	Months = { "Jan": 0, "Feb": 1, "Mar": 2, "Apr": 3, "May": 4, "Jun": 5, "Jul": 6, "Aug": 7, "Sep": 8, "Oct": 9, "Nov": 10, "Dec": 11 };

	dCaps = aDate.match(/(\d{2})-([A-Z][a-z]{2})-(\d{4})/);
	if (!dCaps || dCaps.length !== 4) {
		return undefined;
	}
	tCaps = aTime.match(/(\d{1,2}):+(\d{1,2}):+(\d{1,2})/);
	if (!tCaps || tCaps.length !== 4) {
		return undefined;
	}
	year = parseInt(dCaps[3], 10);
	month = Months[dCaps[2]];
	day = parseInt(dCaps[1], 10);
	hour = parseInt(tCaps[1], 10);
	min = parseInt(tCaps[2], 10);
	sec = parseInt(tCaps[3], 10);
	return new Date(year, month, day, hour, min, sec);
}


function mzData_Path(aPath, aFile) {
	"use strict";
	var pathLast, path = aPath.replace("\\", "/");
	pathLast = aPath.slice(-1);
	if (pathLast === '/') {
		return aPath + aFile;
	}
	return path + '/' + aFile;
}

function msParms_AnyOf() {
	"use strict";
	var i;
	for (i = 0; i < arguments.length; i++) {
		if (arguments[i]) {
			return arguments[i];
		}
	}
	return "";
}

function msParms_Shortest() {
	"use strict";
	var i, stri, shortest = "", shortest_length = 0;
	for (i = 0; i < arguments.length; i++) {
		if (arguments[i] !== undefined && typeof(arguments[i]) === "string") {
			stri =arguments[i].trim();
			if (stri) {
				if (shortest) {
					if (stri.length < shortest_length) {
						shortest = stri;
					}
				} else {
					shortest = stri;
					shortest_length = stri.length;
				}
			}
		}
	}
	return shortest;
}

function msParms_Longest() {
	"use strict";
	var i, stri, longest = "", longest_length = 0;
	for (i = 0; i < arguments.length; i++) {
		if (arguments[i] !== undefined && typeof(arguments[i]) === "string") {
			stri = arguments[i].trim();
			if (stri.length > longest_length) {
				longest = stri;
			}
		}
	}
	return longest;
}

function msParms_SPConcat() {
	"use strict";
	var i, result = "";
	for (i = 0; i < arguments.length; i++) {
		if (arguments[i] && typeof(arguments[i]) === "string") {
			if (result) {
				result += ' ';
			}
			result += arguments[i];
		}
	}
	return result;
}

function brukerBAF_Insrument(aVendor, aName, aSerialNo, aID, aType)
{
	return msParms_AnyOf([aVendor, aName, aSerialNo].join(' '), [aID, aType].join(' '));
}

function brukerBAF_Method(aMethodName, aFilePath) {
	if (aMethodName.trim())
		return aMethodName.trim();
	var newPath = aFilePath.replace(/\\/g, "/"),
		info, baseDir, entries;
	info = new FileInfo(newPath);
	if (!info.isDir) {
		newPath = info.path;
	}
	baseDir = new Dir(newPath);
	entries = baseDir.entryList("*.m", Dir.Dirs);
	if (entries && entries.length > 0) {
		info = new FileInfo(entries[0]);
		return info.baseName;
	}
	return undefined;
}

function brukerXmass_Date(aDateStr, aEpochDate)
{
	"use strict";

	if (aDateStr === undefined || aDateStr.toLowerCase() === "unknown") {
		var time = parseInt(aEpochDate, 10), date;
		if (isNaN(time)) {
			return aEpochDate;
		}

		date = new Date(time * 1000);

		return Str.date2ISOString(date);
	}

	return aDateStr;
}

